#include "logindialog.h"
#include "ui_logindialog.h"
#include "mainwindow.h"
#include <QMessageBox>
#include "userdao.h"

loginDialog::loginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::loginDialog)
{
    ui->setupUi(this);
}

loginDialog::~loginDialog()
{
    delete ui;
}

void loginDialog::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();

    bool ok;
    UserEntity ue;
    ue.name = username;
    ue.password = password;
    UserDao::getInstance()->selectUser(ue,ok);
    int userId = UserDao::getInstance()->verifyUser(username, password);
    if(userId != -1)
    {
        MainWindow *mv = new MainWindow(userId,0);
        mv->setAttribute(Qt::WA_DeleteOnClose); // 设置关闭时删除
        mv->show();
        close();
    }
    else
    {
        QMessageBox::warning(this,"警告","用户名或密码错误");
    }
}

void loginDialog::on_pushButton_reg_clicked()
{
    re->show();
}

void loginDialog::on_pushButton_exit_clicked()
{
    close();
}
